import router from "@/router";
import {defineStore} from 'pinia';
import type {Router, RouteRecordRaw} from 'vue-router';
import {generateMainRoute} from "./main-menu";

const buildRouteInfo = (router: Router, routes: RouteRecordRaw[], parent: any = undefined) => {
    routes.forEach(m => {
        if (parent) {
            router.addRoute(parent.name, m);
        } else {
            router.addRoute(m);
        }

        if (m.children && m.children.length > 0) {
            buildRouteInfo(router, m.children, m);
        }
    })
}

export const useStore: any = defineStore('store', {
    state: () => {
        return {
            listPage: true as boolean,
            listRealms: true as boolean,
            realms: undefined,
            tenant: undefined,
            platform: undefined,
            realmsPath: '' as string,
            platformPath: '' as string,
            menus: [] as Array<MenuType>,
            tenantMenu: {} as Record<string, any>,
            platformMenu: undefined,
            // 路由表
            routes: [] as Array<RouteRecordRaw>,
            currents: {} as Record<string, any>,
            activeMenu: {} as Record<string, string>,
            currentWatch: {} as Record<string, any>,
            userAssets: {} as Record<string, any>,
            userData: {} as Record<string, any>,
            products: {} as Record<string, any>,
            homeRoute: '' as string,
            hasRouted: false as boolean,
            topMenuMap: {} as Record<string, any>,
            recentRealms: [] as Array<Record<string, any>>,
            recentPlatforms: [] as Array<Record<string, any>>,
            loginParam: {} as Record<string, any>,
        }
    },
    getters: {
        hasBuildRoute: (state) => {
            return () => {
                return state.hasRouted;
            }
        },
        getMenuRoute: (state) => {
            return () => {
                return state.routes;
            }
        },
        getHomeRoute: (state) => {
            return () => {
                return state.homeRoute;
            }
        },
        getSubMenus: (state) => {
            return (name: string | undefined = undefined) => {
                const path = name || router.currentRoute.value.path;
                const menus = state.menus;

                return menus.find(item => item.routePath.toLowerCase() === path.toLowerCase())?.children || [];
            }
        },
        getPlatformMenu: (state) => {
            return () => {
                return state.platformMenu;
            }
        },
        getTopMenus: (state) => {
            return () => {
                const menus: MenuType[] = [];
                state.menus.forEach(item => {
                    if (item.routeName !== 'realm' &&
                        item.routeName !== 'platform') {
                        menus.push(item);
                    }
                });
                return menus;
            }
        },
        getCurrentRealms: (state) => {
            return () => {
                return state.realms;
            }
        },
        getCurrentTenant: (state) => {
            return () => {
                return state.tenant;
            }
        },
        getCurrentPlatform: (state) => {
            return () => {
                return state.platform;
            }
        },
        getCurrent: (state) => {
            return (name: string) => {
                return state.currents[name];
            }
        },
        getCurrentWatch: (state) => {
            return (name: string) => {
                return state.currentWatch[name];
            }
        },
        getActiveMenu: (state) => {
            return (name: string) => {
                return state.activeMenu[name];
            }
        },
        getUserAssets: (state) => {
            return () => {
                return state.userAssets;
            }
        },
        getUserMenu: (state) => {
            return () => {
                return state.userAssets ? state.userAssets.menus : [];
            }
        },
        getUser: (state) => {
            return () => {
                return state.userData?.user || {};
            }
        },
        getCredential: (state) => {
            return () => {
                return state.userData?.cred || {};
            }
        },
        getRecentPlatforms: (state) => {
            return () => {
                return state.recentPlatforms || {};
            }
        },
        getRecentRealms: (state) => {
            return () => {
                return state.recentRealms || {};
            }
        },
        getLoginParam: (state) => {
            return () => {
                return state.loginParam || {};
            }
        },
    },
    actions: {
        buildRouteMenus(mainLay: any, data: MenuType[], defPage: string) {
            this.menus = data;
            if (this.menus) {
                this.homeRoute = defPage;
                this.routes = generateMainRoute(mainLay, this, data);
            }
        },
        // 添加动态路由，并同步到状态管理器中
        buildRoutes(data: any, router: Router) {
            this.hasRouted = true;
            buildRouteInfo(router, this.routes);
        },
        setCurrentRealm(rlm: any) {
            this.realms = rlm;
        },
        setCurrentPlatform(pl: any) {
            this.platform = pl;
            this.setCurrentTenant(undefined);
        },
        setCurrentTenant(tenant: any) {
            // 处理菜单
            if (this.tenantMenu && this.tenantMenu.meta) {
                this.tenantMenu.hidden = tenant?.id;
            }
            this.tenant = tenant;
        },
        switchToRealms(rlm: any) {
            this.realms = rlm;
            this.listRealms = false;
        },
        switchToPlatform(pl: any) {
            this.setCurrentPlatform(pl);
            this.listPage = false;
        },
        updateCurrent(name: string, item: any, tag: string) {
            this.currents[name] = {
                tag: tag,
                item: item,
            };
        },
        updateActiveMenu(name: string, val: any) {
            this.activeMenu[name] = val;
        },
        updateUserAssets(assets: any) {
            this.userAssets = assets;
        },
        setUserData(data: any): void {
            this.userData = data;
        },
        setTopMenuMap(data: any) : void {
            this.topMenuMap = data;
        },
        addTopMenuMap(name: string, data: any) : void {
            this.topMenuMap[name] = data;
        },
        setRecentPlatforms(data: any) : void {
            this.recentPlatforms = data;
        },
        setRecentRealms(data: any) : void {
            this.recentRealms = data;
        },
        setLoginParam(data: any) : void {
            this.loginParam = data;
        }
    }
});
